package com.codefish.framework.spring.beans.factory.config;

import com.codefish.framework.spring.beans.exception.BeansException;
import com.codefish.framework.spring.core.io.support.PropertiesLoaderSupport;

import java.util.Properties;

/**
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/11/04 下午 01:53
 */
public abstract class PropertyResourceConfigurer extends PropertiesLoaderSupport implements IBeanFactoryPostProcessor {


    @Override
    public void postProcessBeanFactory(IConfigurableListableBeanFactory beanFactory) throws BeansException {
        //加载properties资源
        Properties properties = new Properties();
        loadProperties(properties);
        //处理properties
        processProperties(properties, beanFactory);
        //必要时添加字符串解析器到beanFactory中
        addStringValueResolverIfNecessary(properties, beanFactory);
    }


    /**
     * 将载入的properties信息注入到beanFactory中
     *
     * @param properties  目标属性
     * @param beanFactory 目标beanFactory
     */
    protected abstract void processProperties(Properties properties, IConfigurableListableBeanFactory beanFactory);

    /**
     * 必要时添加字符串解析器到beanFactory中
     *
     * @param properties 资源对象
     * @param beanFactory beanFactory
     */
    protected void addStringValueResolverIfNecessary(Properties properties, IConfigurableListableBeanFactory beanFactory) {
        //nothing to do!
    }
}
